package com.pur.zsms.storemanage.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.fileupload.DiskFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ExcelReader;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterSupplierVo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.ordercustommanage.vo.ExcelVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.storemanage.po.StoreInDetailedPo;
import com.pur.zsms.storemanage.po.StoreMaterDetailPo;
import com.pur.zsms.storemanage.po.StoreMaterPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedValuePo;
import com.pur.zsms.storemanage.po.StoreOutPo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInDetailedVo;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutDetailedVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.storemanage.vo.StoreSupShopingVo;
import com.pur.zsms.storemanage.vo.StoreVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.service.ISupplierService;

@Controller
@RequestMapping(value = "/store")
public class StoreControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(StoreControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "storeService")
	private IStoreService storeService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@RequestMapping(value = "/findStore.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStore(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, StoreVo storeVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = storeService.listStore(pageController, storeVo);
		List<StoreVo> storeVoList = new ArrayList<StoreVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreVo f = new StoreVo();
				f.setStoreHouseId((Integer) item[0]);
				f.setStoreName((String) item[1]);
				f.setStoreAddress((String) item[2]);
				f.setStoreCustId((Integer) item[3]);
				f.setStoreCustName((String) item[4]);
				storeVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/preSaveStore.do", method = { RequestMethod.GET })
	public ModelAndView preSaveStore() {
		Map map = new HashMap();
		return new ModelAndView("/storemanage/addStore", map);
	}

	@RequestMapping(value = "/saveStore.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveStore(StoreVo c) {
		JSONObject json = new JSONObject();
		try {
			boolean flag = storeService.addStore(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("msg", storeService.getMsg());
				json.put("success", false);
			}

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "deleteStore.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteStore(StoreVo c) {
		JSONObject json = new JSONObject();
		try {
			storeService.deleteStore(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	/**
	 * 管理员查看界面 仓库材料
	 */
	@RequestMapping(value = "/findStoreMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, StoreMaterVo storeMaterVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = storeService.listStoreMater(pageController, storeMaterVo);
		List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreMaterVo f = new StoreMaterVo();
				f.setSid((Integer) item[0]);
				f.setStoreHouseId((Integer) item[1]);
				f.setMaterId((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterNum((BigDecimal) item[4]);
				f.setStoreHouseName((String) item[5]);
				f.setMaterName((String) item[6]);

				storeMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 店长查看界面 仓库材料
	 */
	@RequestMapping(value = "/findMyStoreMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findMyStoreMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreMaterVo storeMaterVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		// 设置查询条件 ----下
		UserPo userInfo = (UserPo) session.getAttribute("user");
		storeMaterVo.setUserId((int) userInfo.getUserId());
		// 设置查询条件 ----上

		this.pageList = storeService.listStoreMater(pageController, storeMaterVo);
		List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreMaterVo f = new StoreMaterVo();
				f.setSid((Integer) item[0]);
				f.setStoreHouseId((Integer) item[1]);
				f.setMaterId((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterNum((BigDecimal) item[4]);
				f.setStoreHouseName((String) item[5]);
				f.setMaterName((String) item[6]);

				storeMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 入库
	 */
	@RequestMapping(value = "/addStore.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addStore(StoreMaterVo storeMaterVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {
			JSONArray array = JSONArray.fromObject(storeMaterVo.getMaterdategridList());

			List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				StoreMaterVo tStoreMaterVo = new StoreMaterVo();
				tStoreMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tStoreMaterVo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tStoreMaterVo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tStoreMaterVo.setRemark(object.get("remarkList").toString());

				storeMaterVoList.add(tStoreMaterVo);

			}

			String today = PubFun.getCurrentDate();

			UserPo userInfo = (UserPo) session.getAttribute("user");
			StoreInVo storeInVo = new StoreInVo();
			storeInVo.setCreatDate(today);
			storeInVo.setRemark(storeMaterVo.getRemark());
			storeInVo.setStoreHouseId(storeMaterVo.getStoreHouseId());
			storeInVo.setStoreInState(1);// 状态未确认
			storeInVo.setStoreInWay(1);// 手动入库
			storeInVo.setOrdId(0);
			storeInVo.setSupId(storeMaterVo.getSupId());// 供货商

			int storeInId = storeService.addStoreIn(storeInVo, (int) userInfo.getUserId());
			storeService.addStoreInDetailed(storeMaterVoList, storeInId);

			// storeService.updateStoreMater(storeMaterVoList, userInfo);

			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	/**
	 * 入库审核
	 */
	@RequestMapping(value = "/reviewStoreIn.do", method = { RequestMethod.POST })
	@ResponseBody
	public String reviewStoreIn(StoreInDetailedVo storeInDetailedVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {

			JSONArray array = JSONArray.fromObject(storeInDetailedVo.getMaterdategridList());
			List<StoreInDetailedPo> storeInDetailedPoList = new ArrayList<StoreInDetailedPo>();
			String storeInId = "";

			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				StoreInDetailedPo tStoreInDetailedPo = new StoreInDetailedPo();
				storeInId = object.get("storeInId").toString();
				// tStoreInDetailedPo.setId(Integer.valueOf(object.get("id").toString()));
				tStoreInDetailedPo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tStoreInDetailedPo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tStoreInDetailedPo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tStoreInDetailedPo.setMaterValueUnit(new BigDecimal(object.get("materValue").toString()));
				storeInDetailedPoList.add(tStoreInDetailedPo);
			}

			UserPo userInfo = (UserPo) session.getAttribute("user");

			// storeInDetailedPoList =
			// storeService.getStoreInDetailedPoList(storeInId);
			// storeService.updateStoreMaterDetail(storeInDetailedPoList,
			// userInfo);
			storeService.updateStoreMater(storeInDetailedPoList, userInfo);

			storeService.updateStoreMater(storeInDetailedPoList, userInfo);
			// 改变入库订单状态 已确认 并修改入库详细订单的 实际进货数量
			storeService.updateStoreInForReview(storeInId, storeInDetailedPoList);

			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/findStoreIn.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreIn(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, StoreInVo storeInVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		storeInVo.setCustId((int) userInfo.getUserId());
		this.pageList = storeService.listStoreIn(pageController, storeInVo);
		List<StoreInVo> storeInVoList = new ArrayList<StoreInVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreInVo f = new StoreInVo();
				f.setStoreInId((Integer) item[0]);
				f.setStoreHouseId((Integer) item[1]);
				f.setStoreName((String) item[2]);
				f.setCustId((Integer) item[3]);
				f.setCustName((String) item[4]);
				f.setSupId((Integer) item[5]);
				f.setSupName((String) item[6]);
				f.setRemark((String) item[7]);
				f.setStoreInWay((Integer) item[8]);
				f.setStoreInState((Integer) item[9]);
				f.setOrdId((Integer) item[10]);
				f.setMaterValueAll((BigDecimal) item[11]);
				f.setContent("查看入库详细内容");
				storeInVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeInVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/pulldownSupId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownSupId(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = supplierService.getAllSupPoJsonString(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	@RequestMapping(value = "/pulldownShopId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownShopId(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = supplierService.getAllShopPoJsonString(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	@RequestMapping(value = "/pulldownStoreCustId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownStoreCustId(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = supplierService.getStoreCustUserPoJsonString(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	/**
	 * 进入入库详细订单
	 * */
	@RequestMapping(value = "/goStoreInDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goStoreInDetailed(String storeInId, String materDay) {
		JSONObject json = new JSONObject();
		/*
		 * FoodVo foodVo = new FoodVo(); try {
		 * 
		 * foodVo = foodService.getFoodid(foodId); // 查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		map.put("storeInId", storeInId);
		map.put("materDay", materDay);
		return new ModelAndView("/storemanage/viewReportStoreInCustDetailed", map);
	}

	/**
	 * 进入出库详细订单
	 * */
	@RequestMapping(value = "/goStoreOutDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goStoreOutDetailed(String storeOutId) {
		JSONObject json = new JSONObject();
		/*
		 * FoodVo foodVo = new FoodVo(); try {
		 * 
		 * foodVo = foodService.getFoodid(foodId); // 查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		map.put("storeOutId", storeOutId);
		return new ModelAndView("/storemanage/viewReportStoreOutCustDetailed", map);
	}

	/**
	 * 
	 * 入库详细清单显示
	 * 
	 * */
	@RequestMapping(value = "/findStoreInDetailed.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreInDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreInDetailedVo storeInDetailedVo, HttpSession session, String storeInId) throws GenericException {
		JSONObject json = new JSONObject();
		page = "1";
		rows = "900";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		storeInDetailedVo.setStoreInId(Integer.valueOf(storeInId));
		this.pageList = storeService.listStoreInDetialed(pageController, storeInDetailedVo);
		List<StoreInDetailedVo> storeInDetailedVoList = new ArrayList<StoreInDetailedVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreInDetailedVo f = new StoreInDetailedVo();
				f.setStoreInId((Integer) item[0]);
				f.setMaterId((Integer) item[1]);
				f.setMaterName((String) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterNum((BigDecimal) item[4]);
				f.setId((Integer) item[5]);
				f.setMaterNumNeed((BigDecimal) item[6]);
				f.setStoreInWay((Integer) item[7]);
				f.setStoreInState((Integer) item[8]);
				f.setUnit((String) item[9]);
				f.setSpecifications((String) item[10]);
				f.setBrands((String) item[11]);
				f.setMaterValue((BigDecimal) item[12]);
				f.setMaterValueUnit((BigDecimal) item[13]);

				storeInDetailedVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeInDetailedVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 
	 * 入库详细清单显示
	 * 
	 * */
	@RequestMapping(value = "/findStoreOutDetailed.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreOutDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreOutDetailedVo storeOutDetailedVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		page = "1";
		rows = "900";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		// storeOutDetailedVo.setStoreOutId(Integer.valueOf(storeOutId));
		this.pageList = storeService.listStoreOutDetialed(pageController, storeOutDetailedVo);
		List<StoreOutDetailedVo> storeOutDetailedVoList = new ArrayList<StoreOutDetailedVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreOutDetailedVo f = new StoreOutDetailedVo();
				f.setStoreOutId((Integer) item[0]);
				f.setMaterId((Integer) item[1]);
				f.setMaterName((String) item[2]);
				f.setMaterNum((BigDecimal) item[3]);
				f.setMaterType((Integer) item[4]);
				f.setMaterValueUnit((BigDecimal) item[5]);
				f.setMaterValue((BigDecimal) item[6]);
				f.setUnit((String) item[7]);
				f.setSpecifications((String) item[8]);
				f.setBrands((String) item[9]);
				storeOutDetailedVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeOutDetailedVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findSelectStoreMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findSelectStoreMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreMaterVo storeMaterVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");

		storeMaterVo.setUserId((int) userInfo.getUserId());

		this.pageList = storeService.listSelectStoreMater(pageController, storeMaterVo);
		List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreMaterVo f = new StoreMaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setBrands((String) item[3]);
				// f.setMaterValue((BigDecimal) item[4]);
				f.setSpecifications((String) item[4]);
				f.setUnit((String) item[5]);
				f.setMaterNum((BigDecimal) item[6]);
				f.setSid((Integer) item[7]);
				storeMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 出库
	 */
	@RequestMapping(value = "/minStore.do", method = { RequestMethod.POST })
	@ResponseBody
	public String minStore(StoreMaterVo storeMaterVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {
			JSONArray array = JSONArray.fromObject(storeMaterVo.getMaterdategridList());

			List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				StoreMaterVo tStoreMaterVo = new StoreMaterVo();
				tStoreMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tStoreMaterVo.setMaterNum(new BigDecimal(object.get("materNumOut").toString()));
				storeMaterVoList.add(tStoreMaterVo);
			}

			String today = PubFun.getCurrentDate();

			UserPo userInfo = (UserPo) session.getAttribute("user");

			// 校验是否该库存在 未确认订单
			if (!storeService.checkStoreOutHaveConfirm(userInfo.getUserId())) {
				json1.put("success", false);
				json1.put("msg", "msg001");
				return json1.toString();
			}

			StoreOutVo storeOutVo = new StoreOutVo();
			storeOutVo.setCreatDate(today);
			storeOutVo.setRemark(storeMaterVo.getRemark());
			storeOutVo.setStoreHouseId(storeMaterVo.getStoreHouseId());
			storeOutVo.setStoreOutState(1);// 状态未确认
			storeOutVo.setStoreOutWay(1);// 手动入库

			int storeOutId = storeService.minStoreOut(storeOutVo, userInfo);

			storeService.minStoreOutDetailed(storeMaterVoList, storeOutId);

			// storeService.updateStoreMaterMin(storeMaterVoList, userInfo);

			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	/**
	 * 出库审核
	 */
	@RequestMapping(value = "/reviewStoreOut.do", method = { RequestMethod.POST })
	@ResponseBody
	public String reviewStoreOut(String storeOutId, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			// // 先取出所需要的数据 以备后续逻辑计算
			// 出库单
			List<StoreOutDetailedPo> storeOutDetailedPolist = storeService.getStoreOutDetailedPoList(storeOutId);
			// 出库总单
			StoreOutPo storeOutPo = storeService.getStoreOutPoByStoreOutId(storeOutId);
			// 库存清单
			List<StoreMaterPo> storeMaterPoList = storeService.getStoreMaterPoList(userInfo.getUserId());
			// 库存详细清单
			List<StoreMaterDetailPo> storeMaterDetailPolist = storeService.getStoreMaterDetailPoList(userInfo.getUserId());

			// 生成 出库 价格分表 计算到底出库什么价格材料
			List<StoreOutDetailedValuePo> storeOutDetailedValuePoList = storeService.addStoreOutDetalValue(storeOutId, storeOutDetailedPolist,
					storeMaterDetailPolist);
			// 计算库存
			storeService.updateStoreMaterMin(storeOutDetailedPolist, storeMaterPoList, storeMaterDetailPolist, storeOutDetailedValuePoList);
			// 更出库总表的状态 和生生消耗的总价格
			storeService.updateStoreOutPoForReviewStoreOut(storeOutDetailedValuePoList, storeOutPo);

			/*
			 * List<StoreOutDetailedPo> storeOutDetailedPoList = null;
			 * storeOutDetailedPoList =
			 * storeService.getStoreOutDetailedPoList(storeOutId);
			 * storeService.updateStoreMaterMin(storeOutDetailedPoList,
			 * userInfo); // 改变出库订单状态 已确认
			 * storeService.updateStoreOut(storeOutId);
			 */
			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/findStoreOut.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreOut(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, StoreOutVo storeOutVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		storeOutVo.setCustId((int) userInfo.getUserId());
		this.pageList = storeService.listStoreOut(pageController, storeOutVo);
		List<StoreOutVo> storeOutVoList = new ArrayList<StoreOutVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreOutVo f = new StoreOutVo();
				f.setStoreOutId((Integer) item[0]);
				f.setStoreHouseId((Integer) item[1]);
				f.setStoreName((String) item[2]);
				f.setCustId((Integer) item[3]);
				f.setCustName((String) item[4]);
				f.setRemark((String) item[5]);
				f.setStoreOutWay((Integer) item[6]);
				f.setStoreOutState((Integer) item[7]);
				f.setCreatDate((String) item[8]);
				f.setContent("查看出库详细内容");
				storeOutVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeOutVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/importBrandSort.do", method = { RequestMethod.POST })
	@ResponseBody
	public String importBrandSort(ExcelVo excelVo, HttpServletRequest request, HttpServletResponse response) throws Exception {

		JSONObject json1 = new JSONObject();
		MultipartFile filename = excelVo.getFilename();

		String temp = request.getSession().getServletContext().getRealPath(File.separator) + "temp"; // 临时目录
		File tempFile = new File(temp);
		if (!tempFile.exists()) {
			tempFile.mkdirs();
		}
		DiskFileUpload fu = new DiskFileUpload();
		fu.setSizeMax(10 * 1024 * 1024); // 设置允许用户上传文件大小,单位:位
		fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:位
		fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
		// 开始读取上传信息
		// int index = 0;
		if (filename == null)
			return null;

		String name = filename.getOriginalFilename();// 获取上传文件名,包括路径
		// name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名
		long size = filename.getSize();
		if ((name == null || name.equals("")) && size == 0)
			return null;
		InputStream in = filename.getInputStream();

		ExcelReader excelReader = new ExcelReader();
		String[] title = excelReader.readExcelTitle(in);
		System.out.println("获得Excel表格的标题:");
		for (String s : title) {
			System.out.print(s + " ");
		}

		// 对读取Excel表格内容测试
		Map<Integer, String> map = excelReader.readExcelContent(in, "*");

		boolean flag = false;
		try {
			flag = storeService.batchSaveMater(map);
		} catch (Exception e) {
			json1.put("msg", storeService.getMsg());
			flag = false;
		}
		// List<BrandMobileInfoEntity> BrandMobileInfos = brandService
		// .importBrandPeriodSort(in);

		// 改为人工刷新缓存KeyContextManager.clearPeriodCacheData(new
		// PeriodDimensions());// 清理所有缓存

		/*
		 * int count = BrandMobileInfos.size();
		 * 
		 * 
		 * String strAlertMsg = ""; if (count != 0) { strAlertMsg = "成功导入" +
		 * count + "条！"; } else { strAlertMsg = "导入失败！"; }
		 */
		// request.setAttribute("brandPeriodSortList", BrandMobileInfos);
		// request.setAttribute("strAlertMsg", strAlertMsg);
		// return null;
		if (flag) {
			json1.put("success", true);
		} else {
			json1.put("success", false);
		}
		return json1.toString();

	}

	@RequestMapping(value = "/testprint.do", method = { RequestMethod.POST })
	@ResponseBody
	public String testprint(ExcelVo excelVo, HttpServletRequest request, HttpServletResponse response) throws Exception {

		JSONObject json1 = new JSONObject();
		MultipartFile filename = excelVo.getFilename();

		String temp = request.getSession().getServletContext().getRealPath(File.separator) + "temp"; // 临时目录
		File tempFile = new File(temp);
		if (!tempFile.exists()) {
			tempFile.mkdirs();
		}
		DiskFileUpload fu = new DiskFileUpload();
		fu.setSizeMax(10 * 1024 * 1024); // 设置允许用户上传文件大小,单位:位
		fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:位
		fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
		// 开始读取上传信息
		// int index = 0;
		if (filename == null)
			return null;

		String name = filename.getOriginalFilename();// 获取上传文件名,包括路径
		// name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名
		long size = filename.getSize();
		if ((name == null || name.equals("")) && size == 0)
			return null;
		InputStream in = filename.getInputStream();

		ExcelReader excelReader = new ExcelReader();
		String[] title = excelReader.readExcelTitle(in);
		System.out.println("获得Excel表格的标题:");
		for (String s : title) {
			System.out.print(s + " ");
		}

		// 对读取Excel表格内容测试
		Map<Integer, String> map = excelReader.readExcelContent(in, "*");

		boolean flag = false;
		try {
			flag = storeService.batchSaveMater(map);
		} catch (Exception e) {
			json1.put("msg", storeService.getMsg());
			flag = false;
		}
		// List<BrandMobileInfoEntity> BrandMobileInfos = brandService
		// .importBrandPeriodSort(in);

		// 改为人工刷新缓存KeyContextManager.clearPeriodCacheData(new
		// PeriodDimensions());// 清理所有缓存

		/*
		 * int count = BrandMobileInfos.size();
		 * 
		 * 
		 * String strAlertMsg = ""; if (count != 0) { strAlertMsg = "成功导入" +
		 * count + "条！"; } else { strAlertMsg = "导入失败！"; }
		 */
		// request.setAttribute("brandPeriodSortList", BrandMobileInfos);
		// request.setAttribute("strAlertMsg", strAlertMsg);
		// return null;
		if (flag) {
			json1.put("success", true);
		} else {
			json1.put("success", false);
		}
		return json1.toString();

	}

}
